//
//  LoginVM.swift
//  LeTu
//
//  Created by gy on 2023/12/13.
//

import Foundation

class LoginVM {
    
    /// 获取验证码
    static func getVerificationCode(phoneNum: String, completeHandle: @escaping NetworkRequestHandle) {
         
        apiProvider.request(api: .getSmsCode(params: ["phone": phoneNum])) { json, jsonArr, code in
            MYLog("获取验证码信息：",json)
            
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            
            completeHandle(dialog)
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
    }
    
    /// 手机号验证码登录
    func mobileAuthCodeLogin(params: [String: Any], completeHandle: @escaping NetworkRequestHandle) {
                
        apiProvider.request(api: .authCodeLogin(params: params)) { json, jsonArr, code in
            MYLog("验证码登录完成：",json)
            
            var dialog = DialogMessage()
            dialog.code = code
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            // 解析data数据
            guard let data = pm.data as? [String : Any], let responseModel = Mapper<LoginResp>().map(JSON: data), pm.code == 200 && responseModel.token.count > 0 else { return completeHandle(dialog) }

            // 保存用户信息
            kUserManager.loginSaveToken(resp: responseModel)
            completeHandle(dialog)
            
            DispatchQueue.main.async {
                // 恢复未完成的交易
                ApplePayManager.shared.restoreTransactions()
            }
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
                
    }
    
    /// 账号密码登录
    func accountPasswordLogin(params: [String: Any], completeHandle: @escaping NetworkRequestHandle) {
                
        apiProvider.request(api: .accountLogin(params: params)) { json, jsonArr, code in
            MYLog("账号密码登录完成：",json)
            
            var dialog = DialogMessage()
            
            guard code == 200, let pm = Mapper<ResponseModel>().map(JSON: json) else { return completeHandle(dialog) }
            dialog.code = pm.code
            dialog.message = pm.msg
            // 解析data数据
            guard let data = pm.data as? [String : Any], let responseModel = Mapper<LoginResp>().map(JSON: data), pm.code == 200 && responseModel.token.count > 0  else { return completeHandle(dialog) }
            
            // 保存用户信息
            kUserManager.loginSaveToken(resp: responseModel)
            completeHandle(dialog)
            
            DispatchQueue.main.async {
                // 恢复未完成的交易
                ApplePayManager.shared.restoreTransactions()
            }
            
        } failureHandler: { error in
            completeHandle(DialogMessage())
        }
                
    }
    
}
